//
//  VideoViewCell.swift
//  SwitfTest
//
//  Created by runone02 on 2019/1/18.
//  Copyright © 2019年 liqiyun. All rights reserved.
//

import UIKit
import Photos

class VideoViewCell: UITableViewCell {

    var videoScrollView: VideoView!
    var btn2: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle(rawValue: 0)!
        self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.setUpUI()
    }
    
    
    
    func setUpUI() {
        self.backgroundColor = UIColor.white
        btn2 = UIButton()
        btn2.setTitle("添加视频", for: .normal)
        btn2.setTitleColor(UIColor.black, for: .normal)
        btn2.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn2.addTarget(self, action: #selector(btn2Click), for: .touchUpInside)
        self.addSubview(btn2)
        btn2.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(self)?.offset()(5)
            make?.centerX.mas_equalTo()(self)
            make?.height.mas_equalTo()(10)
        }
        
        videoScrollView = VideoView(frame: CGRect(x: 0, y: 20, width: SCREEN_WIDTH, height: 100))
        videoScrollView.backgroundColor = UIColor.clear
        self.addSubview(videoScrollView)
        videoScrollView.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(btn2.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(self)
            make?.right.mas_equalTo()(self)
            make?.bottom.mas_equalTo()(self)
        }
        
        
        // 先拼接一个图片
        self.videoScrollView.picArr.append(UIImage(named: "icon_tabBar")!)
        // 点击了删除，更新界面
        self.videoScrollView.closeBtnClickClouse = {[weak self] (imageArr) in
            self?.videoScrollView.picArr = imageArr
        }

        // 访问相册,完成后将asset对象转换成AVPlayeritem对象
        self.videoScrollView.visitPhotoBtnClickClouse = {[weak self] () in
            self?.setupVideo()
        }
        
    }

    //点击添加视频
    @objc func btn2Click(){
        setupVideo()
    }
    
    //=============================下面是视频的处理===================================
    private func setupVideo() {
        let imagePickTool = CLImagePickerTool()
        imagePickTool.isHiddenImage = true
        imagePickTool.cl_setupImagePickerWith(MaxImagesCount: 5) { (assetArr,cutImage) in
            
            PopViewUtil.share.showLoading()
            
            self.videoScrollView.picArr.removeAll()
            self.videoScrollView.picArr.append(UIImage(named: "takePicture")!)
            
            var imageArr = [UIImage]()
            var index = assetArr.count // 标记失败的次数
            
            var successAssetArr = [PHAsset]()
            
            // 获取原图，异步
            // scale 指定压缩比
            // 内部提供的方法可以异步获取图片，同步获取的话时间比较长，不建议！，如果是iCloud中的照片就直接从icloud中下载，下载完成后返回图片,同时也提供了下载失败的方法
            CLImagePickerTool.convertAssetArrToOriginImage(assetArr: assetArr, scale: 0.1, successClouse: {[weak self] (image,assetItem) in
                imageArr.append(image)
                self?.videoScrollView.picArr.append(image)
                successAssetArr.append(assetItem)
                
                if imageArr.count == index {
                    self?.videoScrollView.assetArr = successAssetArr
                    PopViewUtil.share.stopLoading()
                }
                
                }, failedClouse: { () in
                    index = index-1
                    if imageArr.count == index {
                        self.videoScrollView.assetArr = successAssetArr
                        PopViewUtil.share.stopLoading()
                    }
            })
        }
    }
}
